import java.util.Arrays;

/**
 * 1.元素个数      size( )
 * 2.添加元素      add( )
 * 3.删除元素      remove( )
 * 4.获取         get( )
 * 5.替换         set( )
 * 6.是否存在      contains( )
 * 7.查找元素      indexOf( )
 * 8.是否为空      isEmpty( )
 * 9.toString( )
 * 10.清空        clear( )
 */
public class MyArrayList {
    /**
     * 存元素的数组
     */
    private Object[] objects;
    /**
     * 存储元素的个数
     */
    private int size;
    /**
     * 定义一个静态常量(静态初始化常量)
     */
    private static final int DEFAULT_LENGTH = 5;
    int count = 0; // 扩容次数

    /**
     * 无参构造
     */
    public MyArrayList() {
        objects = new Object[DEFAULT_LENGTH];
    }
    /**
     * 有参构造
     */
    public MyArrayList(int size) {
        if (size < 0) {
            throw new RuntimeException("ArrayList集合的大小不能为负数");
        }
        objects = new Object[size];
    }
    /**
     * 元素个数
     */
    public int size() {
        return this.size;
    }
    /**
     * 添加元素
     */
    public boolean add(Object o) {
        if (size == objects.length) {
            grow();
        }
        objects[size] = o;
        size++;
        return true;
    }
    public void add(int index,Object o) {
        if (index < 0 || index > (size-1) ) {
            throw new IndexOutOfBoundsException("下标不合法");
        }
        if (size == objects.length) {
            grow();
        }
        System.arraycopy(objects,index,objects,index+1,size-index);
        objects[index] = o;
        size++;
    }

    /**
     * 扩容
     */
    public void grow() {

//        Object[] newArray = new Object[(objects.length + objects.length / 2)];
//        newArray = Arrays.copyOf(objects,newArray.length);
//        objects = newArray;
        objects = Arrays.copyOf(objects,objects.length + objects.length / 2);
        ++count;
    }
    /**
     * 删除元素
     */
    public boolean remove(Object o) {
        if (contains(o) == false) {
            throw new RuntimeException("参数异常，集合里没有这个元素，无法删除");
        }
        int index = indexOf(o);

        System.arraycopy(objects,index+1,objects,index,size-(index+1));
        objects[size-1] = null;
        --size;
        return true;
    }
    public Object remove(int index) {
        if (index < 0 || index > (size-1) ) {
            throw new IndexOutOfBoundsException("下标不合法");
        }
        objects[index] = null;
        System.arraycopy(objects,index+1,objects,index,size-(index+1));
        size--;
        return objects[index];
    }
    /**
     * 获取元素
     */
    public Object get(int index) {
        if (index < 0 || index > (size-1) ) {
            throw new IndexOutOfBoundsException("下标不合法");
        }
        return objects[index];
    }
    /**
     * 判断是否存在指定元素
     */
    public boolean contains(Object o) {
        for (int i = 0; i < size; i++) {
            if (o.equals(objects[i])) {     //把可能为null的写到equals括号里面
                return true;
            }
        }
        return false;
    }
    /**
     * 替换
     */
    public void set(int index,Object o) {
        objects[index] = o;
    }
    /**
     * 判断是否为空
     */
    public boolean isEmpty() {
        return size == 0;
    }
    /**
     * 查找元素
     */
    //1.
    public int indexOf( Object o) {
        for (int i = 0; i < size; i++) {
            if (objects[i] == null){
                if (objects[i] == o) {
                    return i;
                }
            }else if (objects[i].equals(o)) {
                return i;
            }
        }
        return -1;
    }
    //2.
//    public int indexOf(Object o) {
//        for (int i = 0; i < size; i++) {
//            if (objects[i].equals(o)) {
//                return i;
//            }
//        }
//        return -1;
//    }
    //3.
//    public int indexOf(Object o) {
//        if(contains(o) == true ) {
//            for (int i = 0; i < size; i++) {
//                if (objects[i].equals(o)) {
//                    return i;
//                }
//            }
//        }
//        return -1;
//    }
    public void clear() {
        for (int i = 0; i < size; i++) {
            objects[i] = null;
        }
        size = 0;
    }


    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder("一共扩容了" + count + "次，"+"集合中的");
        for (int i = 0; i < size; i++) {
            sb.append("第").append(i+1).append("个").append("元素是：").append(objects[i] + ", ");
        }
        return sb.substring(0,sb.length()-2);
    }
}
